package homework2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author Administrator
 * @Date 2020/5/19 14:18
 **/
@WebServlet("/answerServlet")
public class AnswerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*设置编码*
         */
        req.setCharacterEncoding("utf-8");
        //获取请求参数
        //单个参数获取，一次性获取所有参数
        String name = req.getParameter("name");
        System.out.println("姓名："+name);
        String[] hobbies = req.getParameterValues("hobby");
        System.out.println("爱好：");
        if (hobbies!=null && hobbies.length>0){
            for (String s:hobbies){
                System.out.println(s+"\t");
            }
        }
        System.out.println();
        System.out.println("获取所有的请求参数");
        Map<String, String[]> map = req.getParameterMap();
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            /*迭代器中取出的元素是key */

            String key = iterator.next();
            String[] values = map.get(key);
            System.out.println(key+":");
            for (String s:values){
                System.out.println(s+"\t");
            }
            System.out.println();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
